import { createRouter, createWebHistory } from "vue-router";

// 导入页面组件
const Layout = () => import("../layouts/baseLayout.vue");

const routes = [
  {
    path: "/",
    redirect: "/admin/draw",
  },
  {
    path: "/admin",
    name: "Admin",
    component: Layout,
    meta: { title: "抽奖管理" },
    redirect: "/admin/draw",
    children: [
      {
        path: "draw",
        name: "DrawAdmin",
        component: () => import("../views/admin/Index.vue"),
        meta: { title: "活动管理" },
      },
      {
        path: "prize",
        name: "PrizeAdmin",
        component: () => import("../views/admin/Prize.vue"),
        meta: { title: "奖品管理" },
      },
      {
        path: "user",
        name: "UserAdmin",
        component: () => import("../views/admin/User.vue"),
        meta: { title: "用户管理" },
      },
      {
        path: "records",
        name: "RecordsAdmin",
        component: () => import("../views/admin/Records.vue"),
        meta: { title: "抽奖记录" },
      },
    ],
  },
  {
    path: "/draw",
    name: "Draw",
    component: () => import("../views/Draw.vue"),
    meta: { title: "抽奖界面" },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由前置守卫，设置页面标题
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;
